//
//  ESFacialPageController.m
//  SportsGather
//
//  Created by fuyoufang on 15/10/17.
//  Copyright (c) 2015年 Egymcom. All rights reserved.
//

#import "ESFacialPageController.h"
#import "ESFacialPanelController.h"

@interface ESFacialPageController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (nonatomic, copy) void (^didSelectEmoji) (NSTextAttachment *);
@property (nonatomic, copy) void (^deleteEmoji)();

@property (nonatomic,strong) NSArray *facialTypeKeys;
@property (nonatomic,assign) NSUInteger facialType;
@property (nonatomic,assign) NSUInteger facialCurrentPage;
@end

@implementation ESFacialPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    // 添加第一个表情 VC
    ESFacialPanelController *emojiPanelVC = [[ESFacialPanelController alloc] init];
    emojiPanelVC.didSelectEmoji = self.didSelectEmoji;
    emojiPanelVC.deleteEmoji    = self.deleteEmoji;
    emojiPanelVC.facialType = 0;
    emojiPanelVC.fromIndex = 0;
    if (emojiPanelVC != nil) {
        self.dataSource = self;
        [self setViewControllers:@[emojiPanelVC]
                       direction:UIPageViewControllerNavigationDirectionReverse
                        animated:NO
                      completion:nil];
    }
}

- (void)setFacialDict:(NSMutableDictionary *)facialDict {
    _facialDict = facialDict;
    self.facialTypeKeys = facialDict.allKeys;
    self.facialType = 0;
    self.facialCurrentPage = 0;
    [facialDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSArray *facials = obj;
        if (facials.count > 0) {
            ESFacialPanelController *emojiPanelVC = [self.childViewControllers lastObject];
            NSRange range;
            range.location = 0;
            range.length = facials.count > 20 ? 20 : facials.count;
            emojiPanelVC.facials = [facials subarrayWithRange:range];
            [self setViewControllers:@[emojiPanelVC]
                           direction:UIPageViewControllerNavigationDirectionReverse
                            animated:NO
                          completion:nil];
        }
        *stop = YES;
    }];
    [self reloadInputViews];
    
}

- (instancetype)initWithTextView:(UITextView *)textView {
    self = [super initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                    navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                  options:nil];
    if (self) {
        _didSelectEmoji = ^(NSTextAttachment *textAttachment) {
            NSAttributedString *emojiAttributedString = [NSAttributedString attributedStringWithAttachment:textAttachment];
            NSMutableAttributedString *mutableAttributeString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
            [mutableAttributeString replaceCharactersInRange:textView.selectedRange withAttributedString:emojiAttributedString];
            textView.attributedText = mutableAttributeString;
//            textView.textColor = [UIColor titleColor];
            [textView insertText:@""];
            textView.font = [UIFont systemFontOfSize:16];
        };
        _deleteEmoji = ^ {
            [textView deleteBackward];
        };
        
        self.dataSource = self;
        self.delegate = self;
    }
    
    return self;
}
- (void)setCurrentFacialType:(NSString *)type {
    ESFacialPanelController *emojiPanelVC = [[ESFacialPanelController alloc] init];
    emojiPanelVC.didSelectEmoji = self.didSelectEmoji;
    emojiPanelVC.deleteEmoji    = self.deleteEmoji;
    emojiPanelVC.fromIndex = 0;
    NSUInteger facialType = 0;
    for (; facialType < self.facialTypeKeys.count; facialType++) {
        if ([self.facialTypeKeys[facialType] isEqualToString:type]) {
            break;
        }
    }
    if (facialType >= self.facialTypeKeys.count) {
        return;
    }
    
    NSArray *nextFacials = self.facialDict[type];
    emojiPanelVC.facialType = facialType;
    NSRange rang;
    rang.location = 0;
    rang.length = 20 < nextFacials.count ? 20 : nextFacials.count;
    emojiPanelVC.facials = [nextFacials subarrayWithRange:rang];
    
  
    self.facialCurrentPage = 0;
    self.facialType = facialType;
    [self setViewControllers:@[emojiPanelVC] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL finished) {
    }];
}

#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(ESFacialPanelController *)vc {

    NSUInteger fromIndex = vc.fromIndex;
    NSUInteger facialType = vc.facialType;
    NSString *facialTypeKey = self.facialTypeKeys[facialType];
    NSArray *facials = self.facialDict[facialTypeKey];
    NSUInteger facialTypeCount = facials.count;
    if (fromIndex == 0) {
        if (vc.facialType != self.facialType) {
            self.facialCurrentPage = 0;
            self.facialType = vc.facialType;
            [pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL finished) {
                //[firstEmojiPanelVC removeFromParentViewController];
            }];
        }
    }
    
    if (facialType == (self.facialTypeKeys.count - 1)
        && ((fromIndex + 1) * 20 ) >= facialTypeCount) {
        return nil;
    }

    NSUInteger nextFacialType = facialType;
    NSUInteger nextFromIndex = fromIndex + 1;
    NSString *nextFacialTypeKey = facialTypeKey;
    if (((fromIndex + 1) * 20 ) > facialTypeCount) {
        nextFacialType++;
        nextFromIndex = 0;
        nextFacialTypeKey = self.facialTypeKeys[nextFacialType];
         [self reloadInputViews];
    }
    NSArray *nextFacials = self.facialDict[nextFacialTypeKey];
    
    ESFacialPanelController *emojiPanelVC = [[ESFacialPanelController alloc] init];
    emojiPanelVC.didSelectEmoji = self.didSelectEmoji;
    emojiPanelVC.deleteEmoji    = self.deleteEmoji;
    emojiPanelVC.fromIndex = nextFromIndex;
    emojiPanelVC.facialType = nextFacialType;
    NSRange rang;
    rang.location = nextFromIndex * 20;
    rang.length = (nextFromIndex * 20) + 20 < nextFacials.count ? 20 : (nextFacials.count - (nextFromIndex * 20));
    emojiPanelVC.facials = [nextFacials subarrayWithRange:rang];

    return emojiPanelVC;
    
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(ESFacialPanelController *)vc {
    NSUInteger fromIndex = vc.fromIndex;
    NSUInteger facialType = vc.facialType;
    NSString *facialTypeKey = self.facialTypeKeys[facialType];
    NSArray *facials = self.facialDict[facialTypeKey];
    
    if (fromIndex == (facials.count - 1) / 20) { // 如果为最后一页
        if (vc.facialType != self.facialType) {
            self.facialCurrentPage = fromIndex;
            self.facialType = vc.facialType;
            [pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
        }
    }
    
    if (facialType == 0 && fromIndex == 0) {
        return nil;
    }

    NSUInteger nextFacialType = facialType;
    NSUInteger nextFromIndex = fromIndex - 1;
    NSString *nextFacialTypeKey = facialTypeKey;
    
    NSArray *nextFacials = facials;
    if (fromIndex == 0) {
        nextFacialType = facialType - 1;
        nextFacialTypeKey = self.facialTypeKeys[nextFacialType];
        nextFacials = self.facialDict[nextFacialTypeKey];
        nextFromIndex = (nextFacials.count - 1) / 20;
    }

    ESFacialPanelController *emojiPanelVC = [[ESFacialPanelController alloc] init];
    emojiPanelVC.didSelectEmoji = self.didSelectEmoji;
    emojiPanelVC.deleteEmoji    = self.deleteEmoji;
    emojiPanelVC.fromIndex = nextFromIndex;
    emojiPanelVC.facialType = nextFacialType;
    NSRange rang;
    rang.location = nextFromIndex * 20;
    rang.length = (nextFromIndex * 20) + 20 < nextFacials.count ? 20 : (nextFacials.count - (nextFromIndex * 20));
    emojiPanelVC.facials = [nextFacials subarrayWithRange:rang];
    return emojiPanelVC;
}


- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    NSArray *facials = self.facialDict[self.facialTypeKeys[self.facialType]];
    return (facials.count + 20 - 1) / 20;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return self.facialCurrentPage;
}
@end
